package caat.application.managedbean;

import caat.application.dao.AdminDAO;
import caat.application.entityobject.AdminEO;
import caat.framework.AppAction;
import caat.framework.AppConstants;
import caat.framework.datamanager.Parameter;
import java.io.Serializable;
import java.sql.Connection;
import java.util.Vector;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;

@SuppressWarnings("serial")
public class AdminLoginMB extends AppAction implements Serializable {
	/*
	 * Khai bao cac bien su dung de login
	 */
	private String username_;
	private String password_;
	private static String STRING_EMPTY = "";
	
	public String getUsername_() {
		return username_;
	}

	public void setUsername_(String username_) {
		this.username_ = username_;
	}

	public String getPassword_() {
		return password_;
	}

	public void setPassword_(String password_) {
		this.password_ = password_;
	}

	public AdminLoginMB() {
		super();
		// TODO Auto-generated constructor stub		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String doLogin() {
		Connection conn = null;
		Vector params = null;
		StringBuffer whereClause = null;
		AdminDAO adminDAO = null;
		AdminEO adminEO = null;
		String result=AppConstants.ACTION_FAILURE;
		try {
			conn = getConnection();
			if (conn != null) {
				// khoi tao cac bien su dung trong method
				whereClause = new StringBuffer();
				params = new Vector();
				adminDAO = new AdminDAO(conn);
				// kiem tra neu nhap user va pass 
				if (username_ != null && password_ != null
						&& !STRING_EMPTY.equals(username_)
						&& !STRING_EMPTY.equals(password_)) {
					whereClause.append("a.username = ? and a.password=?");
					params.add(new Parameter(username_, true));
					params.add(new Parameter(password_, true));
				}
				adminEO = adminDAO.getAdmin(whereClause.toString(), params);
				// kiem tra neu co gia tri thi login success
				if(adminEO!=null){
					result =AppConstants.ACTION_SUCCESS;
				}else{
					// user khong ton tai
					if(username_!=null){						
						String errorSummary= "User Fail!";
						String errorDetail = "User Fail!";
						displayError(errorSummary, errorDetail);
					}
					String errorSummary= "Login Fail!";
					String errorDetail = "Login Fail!";
					displayError(errorSummary, errorDetail);
				}
			}
			
		} catch (Exception ex) {
			ex.printStackTrace();
			result ="errors";
		}
		return result;
	}
	//check confirmpassword (add new)
    public void mvalidatePass(FacesContext context, UIComponent component, Object value) throws ValidatorException {
    	FacesMessage msg;
        password_ = value.toString();
        if (AppConstants.STRING_EMPTY.equals(password_)) {
            msg = new FacesMessage(" pass is not correct");
            throw new ValidatorException(msg);
        }
    }
}
